package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.IHotArticleService;
import com.heima.behavior.dto.ArticleStreamMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class HotArticleResultListener {

    @Autowired
    private IHotArticleService hotArticleService;

    /**
     * 监听实时流式计算热点文章处理结果话题，更新缓存redis，更新数据库文章操作字段
     *
     * @param record
     */
    @KafkaListener(topics = {"${topic.hotArticleResultTopic}"})
    public void handleMessage(ConsumerRecord<String, String> record) {
        if (StringUtils.isNotBlank(record.value())) {
            log.info("流式计算结果话题数据：{}", record.value());
            ArticleStreamMessage articleStreamMessage = JSON.parseObject(record.value(), ArticleStreamMessage.class);
            hotArticleService.updateCacheAndInfo(articleStreamMessage);
        }
    }
}
